<template>
  <div class="app-container">
    <!-- <h3>药品种类管理</h3> -->
    <!-- 查询框 -->
    <div class="filter-container">
      <el-input
        v-model="pageParam.model.realName"
        style="width: 200px;"
        placeholder="病人姓名查询..."
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @input="handleFilter"
      />
    </div>

    <!-- 表格 -->
    <el-table :data="list" fit highlight-current-row style="width: 100%;" @sort-change="sortChange">

      <el-table-column class="filter-item" type="expand">
        <template slot-scope="{row}">
          <el-form label-position="left" inline class="demo-table-expand">

            <el-form-item label="真实姓名">
              <span>{{ row.realName }}</span>
            </el-form-item>

            <el-form-item label="年龄">
              <span>{{ row.age }}</span>
            </el-form-item>

            <el-form-item label="性别">
              <span>{{ row.gender=='0'?'女':'男' }}</span>
            </el-form-item>

            <el-form-item label="手机号">
              <span>{{ row.tellphone }}</span>
            </el-form-item>

            <el-form-item label="紧急联系人">
              <span>{{ row.relaName }}</span>
            </el-form-item>

            <el-form-item label="紧急电话">
              <span>{{ row.relaPhone }}</span>
            </el-form-item>
            <br>
            <br>
            <el-alert type="warning" :closable="false">
              <h3>病例</h3>
              <el-form-item label="检查时间">
                <span>{{ row.record.checkTime }}</span>
              </el-form-item>
              <el-form-item label="病症">
                <span>{{ row.record.symptom }}</span>
              </el-form-item>
              <el-form-item label="诊断">
                <span>{{ row.record.diagnosis }}</span>
              </el-form-item>
              <el-form-item label="片子">
                <el-image :src="row.record.images" />
              </el-form-item>
            </el-alert>
          </el-form>
        </template>
      </el-table-column>

      <!-- 表格字段 -->
      <el-table-column prop="record.doctorName" label="主治医生" />
      <el-table-column prop="realName" label="姓名" />
      <el-table-column prop="gender" label="性别">
        <template slot-scope="scope">
          <span v-if="scope.row.gender==0">女</span>
          <span v-if="scope.row.gender==1">男</span>
        </template>
      </el-table-column>
      <el-table-column prop="tellphone" label="手机号" />

      <el-table-column label="操作" fit>
        <template slot-scope="scope">
          <!--					<el-button size="small" type="primary" v-if="record.id!=''" @click="handleUpdate(scope.row)">编辑病历</el-button> -->
          <!-- <el-button size="small" type="danger" @click="handleDelete(row,$index)">删除</el-button> -->
          <el-button size="small" type="primary" @click="handleUpdate(scope.row)">编辑病历</el-button>
        </template>
      </el-table-column>

    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.pageNum"
      :limit.sync="listQuery.pageSize"
      @pagination="getListByPage"
    />

    <!-- 弹窗组件 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="record"
        label-position="right"
        label-width="150px"
        style="width: 400px; margin-left:50px;"
      >

        <!-- 弹窗-->
        <el-form-item label="疾病症状" prop="symptom">
          <el-input v-model="record.symptom" />
        </el-form-item>
        <el-form-item label="医生诊断" prop="diagnosis">
          <el-input v-model="record.diagnosis" />
        </el-form-item>
        <el-form-item label="过敏史" prop="allergy">
          <el-input v-model="record.allergy" />
        </el-form-item>
        <el-form-item label="传染性" prop="isInfectious">
          <template>
            <el-radio v-model="record.isInfectious" label="0">否</el-radio>
            <el-radio v-model="record.isInfectious" label="1">是</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="是否复诊" prop="isRecheck">
          <template>
            <el-radio v-model="record.isRecheck" label="0">否</el-radio>
            <el-radio v-model="record.isRecheck" label="1">是</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="检查日期" prop="checkTime">
          <template>
            <div class="block">
              <el-date-picker
                v-model="record.checkTime"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="选择日期"
              />
            </div>
          </template>
        </el-form-item>

        <!-- <el-form-item label="审核状态 0待审核 1待发布 2已删除" prop="checkStatus">
          <el-input v-model="temp.checkStatus" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible3 = false">
          取消
        </el-button>
        <el-button type="primary" @click="updateRecord">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>

</template>

<script>
import {
  searchSomePatientPage
} from '@/api/patient/patient'
import {
  updateOne
} from '@/api/record/record'

// 导入组件  记得声明组件
import Pagination from '@/components/Pagination'

export default {
  components: { // 在这里声明组件
    Pagination
  },
  data() {
    return {
      dialogVisible: false,
      list: [], // 列表数据
      total: 0,
      listQuery: { // 分页 排序  查询参数
        pageNum: 1,
        pageSize: 5
      },
      fieldQuery: { // 按字段查询
        realName: ''
      },
      dialogFormVisible: false, // 弹窗
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      dialogPvVisible: false,
      pvData: [],
      record: {
        id: '',
        patientId: '',
        doctorId: '',
        nurseId: '',
        checkTime: '',
        symptom: '',
        diagnosis: '',
        allergy: '',
        images: '',
        isRecheck: '',
        isInfectious: '',
        isHospitalized: '',
        createTime: '',
        updateTime: '',
        isDelete: ''
      },
      tempImageList: [],
      newsCategoryList: [],
      rules: {},
      pageParam: {
        pageNum: 1,
        pageSize: 5,
        model: {
          realName: ''
        },
        orderParams: [],
        superSearchKeyWord: ''
      },
      roleList: []
    }
  },
  created() {
    this.getListByPage()
  },
  methods: {
    sortChange(val) {
      // console.log("排序参数",val)
      const prop = val.prop
      const order = val.order === 'ascending' ? 'asc' : 'desc'
      const orderParam = prop + ' ' + order
      // console.log(orderParam)
      for (let i = 0; i < this.pageParam.orderParams.length; i++) {
        if (this.pageParam.orderParams[i].indexOf(prop) > -1) {
          this.pageParam.orderParams.splice(i, 1)
        }
      }
      this.pageParam.orderParams.push(orderParam)
      console.log(this.pageParam.orderParams)
      this.getListByPage()
    },
    getListByPage() {
      this.pageParam.pageNum = this.listQuery.pageNum
      this.pageParam.pageSize = this.listQuery.pageSize

      console.log(this.pageParam.orderParams)

      searchSomePatientPage(this.pageParam).then(res => {
        // console.log(res)
        this.list = res.content.list
        this.total = res.content.total
      }).catch(err => {
        console.log(err)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getListByPage()
    },
    handleCreate() {
      this.tempImageList = []
      // this.$refs.imagesUpload.clearFiles()
      this.resetTemp()
      this.getNewsCategoryList()
      // console.log(this.userName)
      this.temp.createUserNickName = this.storeNickName
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleUpdate(record) {
      this.record = Object.assign({}, record.record) // copy obj
      this.dialogStatus = 'update'
      // this.getTempImageList() // 展示商品图片列表
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row, index) {
      const temp = {
        id: row.id
      }

      deleteOne(temp).then(() => {
        this.dialogFormVisible = false
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        this.getListByPage()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          insertOne(this.temp).then(() => {
            this.dialogFormVisible = false
            this.$notify({
              title: 'Success',
              message: 'Created Successfully',
              type: 'success',
              duration: 2000
            })
            this.getListByPage()
          })
        }
      })
    },
    updateRecord() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateOne(this.record).then((res) => {
            alert(this.record)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '编辑成功',
              type: 'success',
              duration: 2000
            })
            this.getListByPage()
          })
        }
      })
    },
    resetTemp() {
      this.temp = {
        id: '',
        recordId: '',
        realName: '',
        age: '',
        password: '',
        gender: '',
        tellphone: '',
        identityNo: '',
        status: '',
        violations: '',
        medicalId: '',
        relaName: '',
        relaPhone: '',
        preMoney: ''
      }
    },
    categoryChange() {
      this.getListByPage()
    }
  }
}
</script>

<style>
	.demo-table-expand {
		font-size: 0;
	}

	.demo-table-expand label {
		width: 90px;
		color: #99a9bf;
	}

	.demo-table-expand .el-form-item {
		margin-right: 0;
		margin-bottom: 0;
		width: 50%;
	}

	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}

	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
